<?php
/**
 * Created by PhpStorm.
 * User: HLG
 * Date: 2019/10/16
 * Time: 14:01
 */
namespace app\api\model;
use app\common\model\UploadFile;
use think\Request;
use think\Db;
class Onsale extends BaseModel
{

    /**
     * @param $uid
     * @return array
     * 挂卖列表
     */

    public function getLists($uid){
        $pageParam['page']=input('get.page','1');
        $where['o.user_id'] = array('neq',$uid);
        $where['o.state'] = array('eq',0);
       // print_r($where);die;
        $list = $this->alias('o')
            ->order('o.create_time desc')
            ->where($where)
            ->paginate(null,false,['query'=>$pageParam]);
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }
    //获取挂卖配置
    public function getConfig(){
        $list=db::name('onsale_config')->find(1);
        $list=json_decode($list['value'],true);
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }
    //获取价格走势图配置
    public function getPriceConfig(){
        $list=db::name('onsale_config')->find(2);
        $timediff = time()-$list['edit_time'];
        $days = intval($timediff/86400);
        $list=json_decode($list['value'],true);
        $values=explode('|',$list['up_price']);
        $string='';
        $n=7;
        if($days>=1){
            foreach ($values as $key=>$value){
                if($key>0 && $key <=6 && $n>0){
                    $arr[date('Y-m-d',strtotime("-$n day"))]=$value;
                }
                if($key>=6){
                    $arr[date('Y-m-d')]=$values[6];
                }
                $n--;
            }
            foreach ($arr as $key=>$value){
                $string.=$value."|";
            }
            $string=substr($string,0,strlen($string)-1);
            $argc['up_price']=$string;
            $save=array();
            $save['value']=json_encode($argc);
            $save['edit_time']=time();
            db::name('onsale_config')->where('id',2)->update($save);
        }else{
            foreach ($values as $key=>$value){
               $arr[date('Y-m-d',strtotime("-$n day"))]=$value;
               $n--;
            }
        }
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$arr];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }
    //查看卖单详情
    public function getOnsaleInfo($id){
        $list=$this->find($id);
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }
    //查看卖单列表
    public function getMaiOnsaleList($uid,$state=1){
        $pageParam['page']=input('get.page','1');
        $where['o.user_id'] = array('eq',$uid);
        $where['o.state'] = array('eq',$state);
        $list = $this->alias('o')
            ->order('o.create_time desc')
            ->where($where)
            ->paginate(null,false,['query'=>$pageParam]);
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }
    //查看买单列表
    public function getBuyOnsaleList($uid,$state=1){
        $pageParam['page']=input('get.page','1');
        $where['o.buy_user_id'] = array('eq',$uid);
        $where['o.state'] = array('eq',$state);
        $list = $this->alias('o')
            ->order('o.create_time desc')
            ->where($where)
            ->paginate(null,false,['query'=>$pageParam]);
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }
    /**
     * @param $uid
     * @param $data
     * @return array
     * 卖单提交
     */
   public function Submission($uid,$data){
       $balance=db::name('balance')->where('user_id',$uid)->find();
       if($balance['balance']<$data['hube']){
           return ['code'=>-1,'msg'=>'余额不足','data'=>''];
       }
       $isok=$this->where(['user_id'=>$uid,'state'=>array('lt',3)])->find();
       if($isok){
           return ['code'=>-1,'msg'=>'订单尚未结束,','data'=>''];
       }
       db::startTrans();
       $config=$this->getConfig();
       $userInfo=db::name('user')->find($uid);
       $dicount=1-$config['data']['dicount']/100;
       $arr = [
           'user_id'=>$uid,
           'hube'=>$data['hube']*$dicount, //扣除手续费后的币数
           'real_hube'=>$data['hube'], //真实充值币数
           'user_name'=>$userInfo['nickName'],
           'user_mobile'=>$userInfo['mobile'],
           'price'=>$data['price'],
           'onsale_no'=>getOrderNo(),
           'sum'=>$data['price']*$data['hube'],
           'create_time'=>time(),
       ];
       $add = $this->insert($arr);
       $balance=db::name('balance')->where(['user_id'=>$uid])->setDec('balance',$data['hube']);
       if($add && $balance){
           db::commit();
           $status = ['code'=>1,'msg'=>'添加成功','data'=>''];
       }else{
           db::rollback();
           $status = ['code'=>0,'msg'=>'添加失败','data'=>''];
       }
       return $status;
   }
   //撤销提单
    public function  decOnsale($onsale_id){
       $data=$this->find($onsale_id);
       if($data['state']!=0){
           return ['code'=>0,'msg'=>'撤销失败,','data'=>''];
       }
       db::startTrans();
       $delete=$this->where(['id'=>$onsale_id])->delete();
       $isok=db::name('balance')->where(['user_id'=>$data['user_id']])->setInc('balance',$data['real_hube']);
       if($delete && $isok){
           db::commit();
           return ['code'=>1,'msg'=>'撤销成功,','data'=>''];
       }else{
           db::rollback();
           return ['code'=>0,'msg'=>'撤销失败,','data'=>''];
       }
    }
    //生成订单
    public function  onSaleOrder($onsale_id,$uid){
        $data=$this->where(['buy_user_id'=>$uid,'state'=>array('lt',3)])->find();
        if($data){
            return ['code'=>0,'msg'=>'尚有订单未完成,无法生成,','data'=>''];
        }
        $userInfo=db::name('user')->find($uid);
        $data=array();
        $data['id']=$onsale_id;
        $data['buy_user_id']=$uid;
        $data['state']=1;
        $data['buy_user_name']=$userInfo['nickName'];
        $data['update_time']=time();
        $updata=db::name('onsale')->update($data);
        if($updata!==false){
            $status = ['code'=>1,'msg'=>'生成成功','data'=>''];
        }else{
            $status = ['code'=>0,'msg'=>'生成失败','data'=>''];
        }
        return $status;
    }
    //确认付款
    public function setOnsaleOrder($onsale_id,$pay_img){
        $data=db::name('onsale')->find($onsale_id);
        if($data['state']==2){
            return ['code'=>-1,'msg'=>'您已付款','data'=>''];
        }
        db::startTrans();
        $update['state']=2;
        $update['id']=$onsale_id;
        $update['pay_img']=$pay_img;
        $update['pay_time']=time();
        $updata=db::name('onsale')->update($update);
        if(db::name('onsale_platform')->find()){
            $platform=db::name('onsale_platform')->where('id',1)->setInc('hube',$data['hube']);
        }else{
            $insert=[
                'id'=>1,
                'hube'=>$data['real_hube']-$data['hube'],
            ];
            $platform=db::name('onsale_platform')->insert($insert);
        }
        if($updata!==false && $platform){
            db::commit();
            $status = ['code'=>1,'msg'=>'付款成功','data'=>''];
        }else{
            db::rollback();
            $status = ['code'=>0,'msg'=>'付款失败','data'=>''];
        }
        return $status;
    }
   //改变订单状态
    public function updataOnsale($data){
        $save['state']=$data['state'];
        $save['id']=$data['id'];
        $update=db::name('onsale')->update($save);
        if($update!==false){
            $status = ['code'=>1,'msg'=>'修改成功','data'=>''];
        }else{
            $status = ['code'=>0,'msg'=>'修改失败','data'=>''];
        }
        return $status;
    }
    //上传图片
    public function  upload(){
       return upload();
    }

}